package main

import "fmt"

func main() {
	fmt.Println(mySqrt(9))
}

func mySqrt(x int) int {
	if x == 0 {
		return 0
	}
	left := 0
	right := x
	for left <= right {
		tmp := left + (right-left)/2
		if stmp, stmp1 := tmp*tmp, (tmp+1)*(tmp+1); stmp <= x && stmp1 > x {
			return tmp
		} else if stmp < x && stmp1 <= x {
			left = tmp + 1
		} else if stmp > x && stmp1 > x {
			right = tmp - 1
		}
	}
	return -1
}

func binarySearch(nums []int, target int) int {
	left := 0
	right := len(nums) - 1
	for left <= right {
		mid := left + (right-left)/2
		if nums[mid] == target {
			return mid
		} else if nums[mid] > target {
			right = mid - 1
		} else if nums[mid] < target {
			left = mid + 1
		}
	}
	return -1
}
